package com.xckj.cycling.framework;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.gyf.immersionbar.ImmersionBar;
import com.xckj.cycling.R;

/**
 * @Author: ChenYao
 * @Describe: Fragment的基类
 * @Date: 2023/3/13 22:05
 */
public abstract class BaseFragment<T> extends Fragment implements View.OnClickListener {
    protected Context mContext;
    protected T dataBinding;
    private ImmersionBar mImmersionBar;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(loadLayout(), null);
        dataBinding = (T) DataBindingUtil.bind(view);
        initData();
        initStatusBar();
        setListener();
        return view;
    }


    /**
     * 加载布局文件
     *
     * @return 布局文件
     */
    abstract protected int loadLayout();

    /**
     * 初始化数据
     */
    abstract protected void initData();

    /**
     * 初始化沉浸式状态栏
     */
    private void initStatusBar() {
        if (isStatusBarEnabled()) {
            getStatusBarConfig().init();
        }
    }

    /**
     * 是否使用沉浸式状态栏
     *
     * @return true or false
     */
    protected boolean isStatusBarEnabled() {
        return true;
    }

    /**
     * 状态栏字体是否深色模式
     *
     * @return true or false
     */
    protected boolean isStatusBarDarkFont() {
        return true;
    }

    /**
     * 初始化沉浸式状态栏
     *
     * @return ImmersionBar
     */
    protected ImmersionBar createStatusBarConfig() {
        return ImmersionBar.with(this)
                /*默认状态栏字体颜色为黑色*/.statusBarDarkFont(isStatusBarDarkFont()).keyboardEnable(false)
                /*指定导航栏背景颜色*/.navigationBarColor(R.color.white)
                /*状态栏字体和导航栏内容自动变色，必须指定状态栏颜色和导航栏颜色才可以自动变色*/.autoDarkModeEnable(true).autoNavigationBarDarkModeEnable(true);
    }

    /**
     * 获取状态栏沉浸的配置对象
     *
     * @return ImmersionBar
     */
    public ImmersionBar getStatusBarConfig() {
        if (null == mImmersionBar) {
            mImmersionBar = createStatusBarConfig();
        }
        return mImmersionBar;
    }

    /**
     * 设置监听
     */
    abstract protected void setListener();

    @Override
    public void onResume() {
        super.onResume();
        if (isStatusBarEnabled()) {
            getStatusBarConfig().init();
        }
    }
}
